package com.ysx.oa.hl.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpClientUtil {


    public static JSONObject call(String url) throws Exception{

        JSONObject obj=null;
        //1.打开浏览器  专门用来发http请求
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2.声明get请求
        //String url="http://localhost:8080/kaoqin/role/sum.do?x="+x+"&y="+y;
        HttpGet httpGet = new HttpGet(url);
        //3.发送请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //4.判断状态码
        if(response.getStatusLine().getStatusCode()==200){
            HttpEntity entity = response.getEntity();
            //使用工具类EntityUtils，从响应中取出实体表示的内容并转换成字符串
            String json = EntityUtils.toString(entity, "utf-8");

            obj = JSON.parseObject(json);
        }
        //5.关闭资源
        response.close();
        httpClient.close();
        return obj;

    }
}
